package com.example.controller;

import com.example.domain.Entry;
import com.example.domain.Result;
import com.example.domain.User;
import com.example.domain.vo.EntryVO;
import com.example.service.EntryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/entry")
public class EntryController extends BaseController {

    @Autowired
    private EntryService entryService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public Result list(Entry entry) {
        PageInfo<EntryVO> page = this.entryService.selectPageList(entry);
        return success(page);
    }

    /**
     * 添加或修改
     */
    @PostMapping("/save")
    public Result save(Entry entry, HttpSession session) {
        User user = (User) session.getAttribute("login");
        this.entryService.save(entry, user);
        return success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Long id) {
        Entry entry = this.entryService.selectById(id);
        return success(entry);
    }

    /**
     * 删除
     */
    @PostMapping("/del/{id}")
    public Result delById(@PathVariable("id") Long id) {
        this.entryService.deleteById(id);
        return success();
    }
}
